home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Language/OS - Multiplatform Resource Library
/
LANGUAGE OS.iso
/
smaltalk
/
manchest.lha
/
MANCHESTER
/
manchester
/
2.2
/
BinaryTree.st
< prev
next >
Wrap
Text File
|
1993-07-24
|
3KB
|
122 lines
" NAME BinaryTree
AUTHOR TPH@cs.man.ac.uk
FUNCTION benchmark program --- builds and walks a tree
ST-VERSIONS 2.2
PREREQUISITES
CONFLICTS
DISTRIBUTION world
VERSION 1.1
DATE 22 Jan 1989
SUMMARY BinaryTree
is a benchmark program which builds and walks a tree,
based on an example in Hope provided by Ian Watson.(2.2). TPH
"!
'From Smalltalk-80, Version 2.2 of July 4, 1987 on 15 February 1988 at 6:48:12 pm'!
Object subclass: #BinaryTreeLeaf
instanceVariableNames: 'contents '
classVariableNames: ''
poolDictionaries: ''
category: 'Parallel-algorithms'!
!BinaryTreeLeaf methodsFor: 'accessing'!
sum
"The receiver is a leaf, so answer with the contents."
^contents! !
!BinaryTreeLeaf methodsFor: 'private'!
setContents: anObject
"Set the contents to anObject."
contents _ anObject! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
BinaryTreeLeaf class
instanceVariableNames: ''!
!BinaryTreeLeaf class methodsFor: 'instance creation'!
with: anObject
"Create a new instance of the receiver, containing anObject."
^self new setContents: anObject! !
'From Smalltalk-80, Version 2.2 of July 4, 1987 on 15 February 1988 at 6:47:53 pm'!
Object subclass: #BinaryTreeNode
instanceVariableNames: 'left right '
classVariableNames: ''
poolDictionaries: ''
category: 'Parallel-algorithms'!
!BinaryTreeNode methodsFor: 'accessing'!
sum
"Walk the tree represented by the receiver, and sum the nodes."
^(1 + (left sum + right sum // 2))! !
!BinaryTreeNode methodsFor: 'private'!
setLeft: leftNode setRight: rightNode
"Set the left and right nodes appropriately."
left _ leftNode.
right _ rightNode! !
"-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- "!
BinaryTreeNode class
instanceVariableNames: ''!
!BinaryTreeNode class methodsFor: 'instance creation'!
grow: aNumber
"Answer with a tree of size aNumber."
(aNumber = 0)
ifTrue: [^BinaryTreeLeaf with: aNumber]
ifFalse: [^self
left: (self grow: aNumber - 1)
right: (self grow: aNumber - 1)]!
left: leftNode right: rightNode
"Create a new instance of the receiver, with references
to leftNode and rightNode."
^self new setLeft: leftNode setRight: rightNode! !
!BinaryTreeNode class methodsFor: 'examples'!
benchmarkDepth: depth repeated: count
"BinaryTreeNode benchmarkDepth: 8 repeated: 10."
Transcript
cr;
show: depth printString;
tab;
show: ((Time millisecondsToRun: [
count timesRepeat: [
(BinaryTreeNode grow: depth) sum]]) / count asFloat) printString!
exampleWorkspace
"Select and execute the expressions below to run the benchmarks."
"Smalltalk growOTBy: 20000." "Needed for large trees."
"Smalltalk oopsLeft."
"
0 to: 10 do: [:num |
BinaryTreeNode benchmarkDepth: num repeated: 30].
11 to: 12 do: [:num |
BinaryTreeNode benchmarkDepth: num repeated: 10].
BinaryTreeNode benchmarkDepth: 13 repeated: 5.
BinaryTreeNode benchmarkDepth: 14 repeated: 1.
"! !